package com.ibiaoke.order.dao;

import com.google.common.collect.Lists;
import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.goods.entity.Goods;
import com.ibiaoke.goods.entity.GoodsExample;
import com.ibiaoke.goods.mapper.GoodsMapper;
import com.ibiaoke.order.entity.ShoppingCart;
import com.ibiaoke.order.entity.ShoppingCartExample;
import com.ibiaoke.order.mapper.ShoppingCartMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by huhaichao on 2017/6/8.
 */
@Repository
public class ShoppingCartDao extends BaseDao<ShoppingCart, ShoppingCartExample, Integer, ShoppingCartMapper> {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private GoodsMapper goodsMapper;


    @Override
    public ShoppingCartMapper getMapper() {
        return shoppingCartMapper;
    }

    /**
     * 购物车列表
     * @param customerId
     * @return
     */
    public List<ShoppingCart> getShoppingCartByCustomerId(Integer customerId) {
        ShoppingCartExample example = new ShoppingCartExample();
        example.createCriteria().andCuntomerIdEqualTo(customerId).andDelStatusEqualTo((byte) CommonConsts.NO);
        return shoppingCartMapper.selectByExample(example);
    }

    public List<ShoppingCart> getShoppingCartByCustomerId(Integer customerId, Page page) {
        ShoppingCartExample example = new ShoppingCartExample();
        example.createCriteria().andCuntomerIdEqualTo(customerId).andDelStatusEqualTo((byte) CommonConsts.NO);

        page.setTotalCount(shoppingCartMapper.countByExample(example));
        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("update_time desc");
        return shoppingCartMapper.selectByExample(example);
    }

    public List<Goods> shoppingListByIds(List<Integer> Ids) {
        if(CollectionUtils.isEmpty(Ids)){
            return Lists.newArrayList();
        }
        GoodsExample example= new GoodsExample();
        example.createCriteria().andGoodsIdIn(Ids).andDelStatusEqualTo(CommonConsts.NO);
        return goodsMapper.selectByExample(example);
    }

    public List<ShoppingCart> listShoppingCart(List<Integer> shoppingCartIds){
        ShoppingCartExample example = new ShoppingCartExample();
        example.createCriteria().andDelStatusEqualTo((byte) CommonConsts.NO).andCartIdIn(shoppingCartIds);
        return shoppingCartMapper.selectByExample(example);
    }

    /**
     * (可批量)删除购物车
     * @param shoppingCartlist
     * @param customerId
     * @return
     */
    public int batchDeleteShoppingCart(List<Integer> shoppingCartlist, Integer customerId) {
        ShoppingCartExample example = new ShoppingCartExample();
        example.createCriteria().andCartIdIn(shoppingCartlist).andCuntomerIdEqualTo(customerId);

        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setDelStatus((byte) CommonConsts.YES);

        return shoppingCartMapper.updateByExampleSelective(shoppingCart, example);
    }

    /**
     * 购物车数量
     * @param customerId
     * @return
     */
    public Long countShoppingCart(Integer customerId) {
        ShoppingCartExample example = new ShoppingCartExample();
        example.createCriteria().andCuntomerIdEqualTo(customerId).andDelStatusEqualTo((byte) CommonConsts.NO);
        return shoppingCartMapper.countByExample(example);
    }

    /**
     * 根据商品Id，客户Id获取购物车信息
     * @param goodsId
     * @param customerId
     * @return
     */
    public ShoppingCart getShoppingCartByCustomerId(Integer goodsId, Integer customerId) {
//        return shoppingCartMapper.selectByGoodsIdAndCustomerIdForUpdate(goodsId,customerId, CommonConsts.NO);
        return null;
    }

    /**
     * 根据购物车Id，客户Id获取购物车信息
     * @param cartId
     * @param customerId
     * @return
     */
    public ShoppingCart getShoppingCartByCartId(Integer cartId, Integer customerId) {
        ShoppingCartExample example = new ShoppingCartExample();
        example.createCriteria().andCartIdEqualTo(cartId).andCuntomerIdEqualTo(customerId).andDelStatusEqualTo((byte) CommonConsts.NO);
        return getByExample(example);
    }

    /**
     * 增减购物车商品数量
     * @param cartId
     * @param customerId
     * @param num
     * @return
     */
    public int updateShoppingCart(Integer cartId, Integer customerId, Integer num) {
        ShoppingCartExample example = new ShoppingCartExample();
        example.createCriteria().andCartIdEqualTo(cartId).andCuntomerIdEqualTo(customerId);
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setNum(num);
        return shoppingCartMapper.updateByExampleSelective(shoppingCart,example);
    }


    public ShoppingCart getShaoppingCartInfo(Integer customerId, Integer shopId, Integer goodsId) {
        ShoppingCartExample example = new ShoppingCartExample();
        example.createCriteria().andCuntomerIdEqualTo(customerId).andShopIdEqualTo(shopId).andGoodsIdEqualTo(goodsId).andDelStatusEqualTo((byte) CommonConsts.NO);
        return getByExample(example);
    }

    public List<ShoppingCart> getShoppingCartInfos(List<Integer> shoppingCartlist, Integer customerId) {
        ShoppingCartExample example = new ShoppingCartExample();
        example.createCriteria().andCartIdIn(shoppingCartlist).andCuntomerIdEqualTo(customerId).andDelStatusEqualTo((byte) CommonConsts.NO);
        return shoppingCartMapper.selectByExample(example);
    }

//    public List<ShoppingCart> newListShoppingCart(Integer customerId) {
//        ShoppingCartExample example = new ShoppingCartExample();
//        example.createCriteria().andCuntomerIdEqualTo(customerId).andDelStatusEqualTo((byte) CommonConsts.NO);
//        return shoppingCartMapper.selectByExample(example);
//    }
}
